package cn.itcast.service.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ContractProductServiceImpl implements ContractProductService {
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;
    @Override
    public void save(ContractProduct contractProduct) {
        // 1 设置一些货物的额外属性（uuid 总金额...）
        contractProduct.setId(UUID.randomUUID().toString());
        Double total=contractProduct.getPrice()*contractProduct.getCnumber();
        contractProduct.setAmount(total);
        //2 保存货物的数据到数据库
        contractProductDao.insertSelective(contractProduct);
        //3 根据货物的所属合同id查询合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //4 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()+total);
        //5 修改合同的货物数
        contract.setProNum(contract.getProNum()+1);
        //6 修改合同数据到数据库
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //1 根据货物的id获取到以前的货物对象
      ContractProduct oldcontractProduct =contractProductDao.selectByPrimaryKey(contractProduct.getId());
      /*获取以前的总金额*/
        Double amount = oldcontractProduct.getAmount();
        //3 计算修改之后的总金额
        Double new_total=  contractProduct.getPrice()*contractProduct.getCnumber();
        contractProduct.setAmount(new_total);
        /*修改货物数据到数据库*/
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
        /*根据货物的合同id获取到合同*/
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        /*设置修改的总金额*/
        contract.setTotalAmount(contract.getTotalAmount()-amount+new_total);
        /*修改合同的数据到数据库*/
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        /*查询货物*/
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        Double amount = contractProduct.getAmount();
        /*删除货物*/
        contractProductDao.deleteByPrimaryKey(id);
        // 附件变
        //1 获取附件对象（货物id）
        ExtCproductExample extCproductExample=new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(id);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);
        //2 统计总金额
        for (ExtCproduct extCproduct : extCproducts) {
            amount=extCproduct.getAmount();
            /*删除附件*/
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }
        /*获取合同*/
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //2 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount()-amount);
        //3 修改合同的货物数
        contract.setProNum(contract.getProNum()-1);
        //4 修改合同的附件数
        contract.setExtNum(contract.getExtNum()-1);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ContractProduct findById(String id) {
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);

        return contractProduct;
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ContractProduct> contractProducts = contractProductDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(contractProducts);
        return pageInfo;
    }
}
